import {Component, OnInit} from '@angular/core';
import {ErrRes} from '../../../../../frame/global/err-res';
import {Permissions, SysRole, SysRoleCondition, SysRoleMenu} from '../../sys-role.type';
import {Services} from '../../../../../frame/module/services';
import {isNullOrUndefined} from 'util';
import {ActivatedRoute, Router} from '@angular/router';
import {ModifyRoleMenuComponent} from '../modify-role-menu.component';
import {TreeNode} from 'primeng/api';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';

@Component({
  selector: 'app-main-modify-role-menu',
  templateUrl: './main-modify-role-menu.component.html',
  styleUrls: ['./main-modify-role-menu.component.css']
})
export class MainModifyRoleMenuComponent implements OnInit {

    errRes = new ErrRes();

    uuid: string;
    allMenuTreeNode: TreeNode[] = [];
    selectedMenu: TreeNode = null;
    selectAllPermission: string[] = [];
    currentMenu: TreeNode;
    selectedMenuUuids: string[] = [];
    selectPermissionUuid: string[] = [];
    allPermissionUuid: Permissions[] = [];
    sysRoledomain: SysRole = new SysRole();

    extend = {
        domain: {},
    };

    func = {
        nodeSelect: (event) => {
            this.currentMenu = event.node;
            Services.sysRole.listRoleMenu(SysRoleMenu.create({
                roleUuid: this.uuid,
                menuUuid : event.node.data
            })).subscribe(item => {
                this.selectedMenuUuids = item.domain.selectMenuUuids || [];
                this.allPermissionUuid = item.domain.allPermissionUuid || [];
                this.selectPermissionUuid = item.domain.selectPermissionUuid || [];
            });
        },
        selectAllPermissionChange: (event) => {
            if (event) {
                this.selectPermissionUuid = [...this.allPermissionUuid.map(item => item.uuid)];
            } else {
                this.selectPermissionUuid = [];
            }
        },
        selectPermissionChange: (event) => {
            if (event) {
                if (this.selectPermissionUuid.length === this.allPermissionUuid.length) {
                    this.selectAllPermission = ['all'];
                }
            } else {
                this.selectAllPermission = [];
            }
        },
        saveRoleMenu: () => {
            if (!this.currentMenu) {
                Zero3Alert.req('请先选中菜单');
                return;
            }
            Services.sysRole.saveRoleMenu(SysRoleMenu.create({
                roleUuid: this.uuid,
                menuUuid: this.currentMenu.data,
                selectMenuUuids: this.selectedMenuUuids,
                unSelectMenuUuids: this.selectedMenuUuids.length > 0 ? [] : [this.currentMenu.data],
                selectPermissionUuid: this.selectPermissionUuid
            }), {
                success: (result) => {
                    Zero3Alert.req('保存成功');
                    console.log('saveRoleMenu success --> %o', result);
                },
                error: (result) => {
                    Zero3Alert.req('保存失败');
                    console.log('saveRoleMenu error --> %o', result);
                }
            }).subscribe();
        },
        loadAllMenu: () => {
            Services.sysMenu.listAllMenu().subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domains)) {
                    this.allMenuTreeNode = this.func.translateMenu2Node(item.domains);
                }
            });
        },
        translateMenu2Node: (menus) => {
            return <TreeNode[]> menus.map(item => {
                return {
                    // id: item.uuid,
                    label: item.serName,
                    data: item.uuid,
                    expandedIcon: 'fa fa-folder-open',
                    collapsedIcon: 'fa fa-folder',
                    icon: isNullOrUndefined(item.children) ? 'fa fa-file-image-o' : null,
                    children: isNullOrUndefined(item.children) ? null : this.func.translateMenu2Node(item.children)
                };
            });
        },
    };

    constructor(public modifyRoleMenu: ModifyRoleMenuComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.uuid = this.route.snapshot.paramMap.get('uuid');
        this.modifyRoleMenu.main = this;
        Services.sysRole.info(SysRoleCondition.create({uuid: this.uuid}))
            .subscribe(item => this.sysRoledomain = item.domain);
        this.func.loadAllMenu();
    }
}
